package com.tengju.user.infrastructure.repository;

import com.tengju.user.domain.model.sign.StarBlacklist;
import com.tengju.user.domain.model.sign.StarBlacklistRepository;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.StarBlacklistDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.StarBlacklistDOCustomMapper;
import com.tengju.user.infrastructure.repository.convert.StarBlacklistConvert;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

@Repository
public class StarBlacklistRepositoryImpl implements StarBlacklistRepository {

    @Resource
    private StarBlacklistDOCustomMapper starBlacklistDOCustomMapper;

    @Override
    public int insert(StarBlacklist starBlacklist) {
        StarBlacklistDO record = StarBlacklistConvert.convert(starBlacklist);
        return starBlacklistDOCustomMapper.insertSelective(record);
    }

    @Override
    public StarBlacklist getByIdCode(Long idCode, String mobile) {
        List<StarBlacklistDO> dos = starBlacklistDOCustomMapper
                .listStarBlackByIdCode(idCode);
        return StarBlacklistConvert
                .convert(CollectionUtils.isEmpty(dos) ? null : dos.get(0), mobile);
    }

    @Override
    public int edit(StarBlacklist starBlacklist) {
        StarBlacklistDO record = StarBlacklistConvert.convert(starBlacklist);
        return starBlacklistDOCustomMapper.updateByIdCodeSelective(record);
    }

    @Override
    public int del(UserIdCode userIdCode) {
        return starBlacklistDOCustomMapper.delete(userIdCode.getValue());
    }

    @Override
    public int count() {
        return starBlacklistDOCustomMapper.count();
    }

    @Override
    public List<StarBlacklist> queryByPage(Integer offset, Integer pageSize) {
        List<StarBlacklistDO> dos = starBlacklistDOCustomMapper.queryByPage(offset, pageSize);
        return dos.stream()
                .map(starBlacklistDO -> StarBlacklistConvert.convert(starBlacklistDO, null))
                .collect(Collectors.toList());
    }
}
